<?php

class Simpanan extends Controller {

	function Simpanan()
	{
		parent::Controller();	
		$this->load->library('auth');
		$this->auth->check_user_authentification();
		$this->load->model('simpanan_model');		
	}
	
	function index()
	{
		$data['title'] = "Daftar Simpanan";
		$data['main_content'] = 'simpanan/display';
		$data['simpanan_data'] = $this->simpanan_model->get_all_data();
		$this->load->view('layout/template', $data);
	}
	
	function analisa()
	{
		$data['title'] = "Analisa Simpanan";
		$data['main_content'] = 'simpanan/display_analisa';
		$data['simpanan_data'] = $this->simpanan_model->get_all_data();
		$this->load->view('layout/template', $data);
	}	

	function add()
	{
		$data['title'] = "Tambah Simpanan";
		$data['main_content'] = 'simpanan/form';
		$data['act'] = 'add';
		$data['form_act'] = 'insert';
		$data['simpanan_data'] = FALSE;
		$data['new_id'] = $this->simpanan_model->get_last_id();
		$this->load->view('layout/template', $data);
	}

	function view()
	{
		$id = $this->uri->segment(3);
		$data['title'] = "Lihat Simpanan";
		$data['main_content'] = 'simpanan/form';
		$data['act'] = 'view';
		$data['form_act'] = '';
		$data['simpanan_data'] = $this->simpanan_model->get_data_by_id($id);
		$this->load->view('layout/template', $data);
	}	

	function edit()
	{
		$id = $this->uri->segment(3);
		$data['title'] = "Edit Simpanan";
		$data['main_content'] = 'simpanan/form';
		$data['act'] = 'edit';
		$data['form_act'] = 'update/'.$id;
		$data['simpanan_data'] = $this->simpanan_model->get_data_by_id($id);
		$this->load->view('layout/template', $data);		
	}
	
	function popup()
	{
		$data['simpanan_data'] = $this->simpanan_model->get_all_data();
		$this->load->view('simpanan/popup', $data);
	}
	
	function insert()
	{
		if (!$this->_simpanan_validation())
		{
			$this->session->set_userdata('ERRMSG_ARR', validation_errors());
			$this->add();
		}
		else
		{
			$this->simpanan_model->fill_data();
			//Cek Akun
			if(!$this->simpanan_model->check_name())
			{
				$this->session->set_userdata('ERRMSG_ARR', 'Rek simpanan telah digunakan');
				$this->add();
			}
			//Insert Data
			elseif($this->simpanan_model->insert_data()) 
			{
				$this->session->set_userdata('SUCCESSMSG', 'simpanan baru sukses');
				redirect('simpanan');
			}
		}
	}

	function update()
	{
		$id = $this->uri->segment(3);
		if (!$this->_simpanan_validation())
		{
			$this->session->set_userdata('ERRMSG_ARR', validation_errors());
			$this->edit();
		}
		else
		{
			$this->simpanan_model->fill_data();
			//Cek Akun
			if(!$this->simpanan_model->check_name($id))
			{
				$this->session->set_userdata('ERRMSG_ARR', 'Rek simpanan telah digunakan');
				$this->edit();
			}		
			//Update Data
			elseif($this->simpanan_model->update_data($id))
			{
				$this->session->set_userdata('SUCCESSMSG', 'Update simpanan sukses ;)');
				redirect('simpanan');
			}
		}
	}

	function delete()
	{
		$id = $this->input->post('id');
		$simpanan_data = $this->simpanan_model->get_data_by_id($id);
		if($this->_check_jurnal_exist($id))
		{
			$msg = 'E#Pimpanan '.$simpanan_data['no_rekening'].' tidak dapat dihapus karena telah dipakai di jurnal.';
		}
		else
		{
			if($this->simpanan_model->delete_data($id))
			{
				$msg = 'S#Simpanan '.$simpanan_data['no_rekening'].' telah dihapus.';
			}
			else
			{
				$msg .= 'E#Terjadi kesalahan dalam menghapus data simpanan '.$simpanan_data['no_rekening'].'. Harap coba lagi.';
			}
		}
		echo $msg;
	}
	
	function _check_jurnal_exist($id)
	{
		$this->load->model('jurnal_model');
		$this->jurnal_model->set_project(0,$id);
		$journal_data = $this->jurnal_model->get_data();
		if ($journal_data)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

	function _simpanan_validation()
	{
		$this->form_validation->set_rules('anggota', 'Anggota', 'trim|required');
			
		return $this->form_validation->run();
	}

}
/* End of file simpanan.php */
/* Location: ./application/controllers/simpanan.php */
